//
//  ViewController.m
//  KVO
//
//  Created by xiewei on 16/6/20.
//  Copyright © 2016年 xiewei. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (nonatomic, strong) Person *person;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.person = [Person new];
    self.person.name = @"weige";
    
    //kvo监听person的name属性的变化
    [self.person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
    
    
}

- (void)dealloc {
    //移除kvo的监听
    [self.person removeObserver:self forKeyPath:@"name"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([object isKindOfClass:[Person class]]) {
        NSLog(@"%@",change);
        
        NSLog(@"%@",[NSThread currentThread]);
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (int i = 0; i < 10; i++) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            //属性的修改在哪个线程  监听的方法就再哪个线程
            self.person.name = [NSString stringWithFormat:@"abc - %d",i];
            NSLog(@"===");
        });
        
    }
}


@end
